package com.dangs.autotest.test;

import android.os.Looper;

import com.dangs.autotest.tool.L;

import java.util.List;

public abstract class BaseTask implements Task {
    protected String KEY = this.getClass().getSimpleName();
    protected String TAG = KEY;
    protected String name = "unknown";
    protected int id = 0;
    protected long startTime = 0;
    protected boolean userStop = false;
    protected boolean checked = false;
    protected String moreInfo = "";
    protected int failTimes = 0;
    protected List<Task> tasks;
    protected TaskCallBack callBack;
    protected int waiteTime = 3000;
    protected int keepTime = 3000;
    boolean isRunning = false;

    BaseTask(String name) {
        this.name = name;
    }

    @Override
    public void prepare() {
        L.w(TAG, "no implements");
    }

    @Override
    public void init() {
        if (callBack != null) {
            callBack.onInit(this);
        }
        tasks = null;
    }

    @Override
    public void start() {
        if (Looper.getMainLooper() == Looper.myLooper()) {
            L.w(TAG, "current thread is UI, don't run");
            return;
        }
        L.i(TAG, name + " start<====");
        isRunning = true;
        callBack.onStart(this);
        startTime = System.currentTimeMillis();
        runInner();
        isRunning = false;
        L.i(TAG, name + " end<====");
    }

    private void runInner() {
        if (waiteTime > 0) {
            try {
                Thread.sleep(waiteTime);
            } catch (InterruptedException e) {
                L.w(TAG, "sleep err:" + e);
            }
        }
        run();
        if (waiteTime > 0) {
            try {
                Thread.sleep(keepTime);
            } catch (InterruptedException e) {
                L.w(TAG, "sleep err:" + e);
            }
        }
        if (callBack != null) {
            callBack.onFinish(this, null);
        }
    }

    @Override
    public void stop() {
        userStop = false;
    }

    @Override
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public int getId() {
        return 0;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setTasks(List<Task> tasks) {
        L.d(TAG, "setTasks:" + tasks);
    }

    @Override
    public List<Task> getTasks() {
        return tasks;
    }

    @Override
    public String getInfo() {
        return moreInfo;
    }

    @Override
    public void setInfo(String info) {
        this.moreInfo = info;
    }

    @Override
    public void check(Boolean check) {
        L.d(TAG, "check:" + tasks);
        checked = check;
        if (callBack != null) {
            callBack.onChecked(this);
        }
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    public void setWaiteTime(int waiteTime) {
        this.waiteTime = waiteTime;
    }

    @Override
    public int getWaiteTime() {
        return waiteTime;
    }

    @Override
    public void setKeepTime(int keepTime) {
        this.keepTime = keepTime;
    }

    @Override
    public int getKeepTime() {
        return keepTime;
    }

    @Override
    public void setTimes(int times) {

    }

    @Override
    public int getTimes() {
        return 0;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        L.d(TAG, name + " finalize");
    }

    @Override
    public void setCallBack(TaskCallBack callBack) {
        L.d(TAG, "setCallBack:" + this);
        this.callBack = callBack;
    }

    @Override
    public boolean isRunning() {
        return isRunning;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("name:").append(name).append(", ")
                .append("checked:").append(checked).append(", ")
                .append("wait time:" + waiteTime).append(", ")
                .append("keep time:").append(keepTime);
        return builder.toString();
    }
}



